package com.yfker.pda.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Locale;

public class TtsUtils {
    private static final String TAG = "TtsUtils";
    private Runnable checkTtsRunnable;
    private Context mContext;
    private Handler mHandler;
    private TtsPlayListener mListener;
    private String mSpeakMessage;
    private TextToSpeech mTextToSpeech;

    /* loaded from: classes.dex */
    public interface TtsPlayListener {
        void onFailed(String str);

        void onSuccess(String str);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static final class SingletonHolder {
        private static final TtsUtils INSTANCE = new TtsUtils();

        private SingletonHolder() {
        }
    }

    public static TtsUtils getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private TtsUtils() {
        this.checkTtsRunnable = new Runnable() { // from class: com.hx.lib_common.utils.TtsUtils.4
            @Override // java.lang.Runnable
            public void run() {
                if (TtsUtils.this.mListener != null) {
                    TtsUtils.this.mListener.onFailed(TtsUtils.this.mSpeakMessage);
                }
            }
        };
    }

    public void init(Context context) {
        this.mContext = context.getApplicationContext();
        this.mTextToSpeech = new TextToSpeech(this.mContext, new TextToSpeech.OnInitListener() { // from class: com.hx.lib_common.utils.TtsUtils.1
            @Override // android.speech.tts.TextToSpeech.OnInitListener
            public void onInit(int i) {
                Log.d(TAG, "onInit: " + i);
                if (i == 0) {
                    int language = TtsUtils.this.mTextToSpeech.setLanguage(Locale.CHINA);
                    if (language == -1 || language == -2) {
                        Toast.makeText(TtsUtils.this.mContext, "语音数据丢失或不支持", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

    public void speak(String str) {
        speak(str, null);
    }

    public void speak(final String str, final TtsPlayListener ttsPlayListener) {
        if (this.mHandler == null) {
            this.mHandler = new Handler(Looper.getMainLooper());
        }
        TextToSpeech textToSpeech = this.mTextToSpeech;
        if (textToSpeech != null && !textToSpeech.isSpeaking()) {
            this.mTextToSpeech.setPitch(1.2f);
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("utteranceId", str);
            this.mTextToSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() { // from class: com.hx.lib_common.utils.TtsUtils.2
                @Override // android.speech.tts.UtteranceProgressListener
                public void onStart(String str2) {
                    TtsUtils.this.mHandler.removeCallbacks(TtsUtils.this.checkTtsRunnable);
                }

                @Override // android.speech.tts.UtteranceProgressListener
                public void onDone(String str2) {
                    TtsPlayListener ttsPlayListener2 = ttsPlayListener;
                    if (ttsPlayListener2 != null) {
                        ttsPlayListener2.onSuccess(str2);
                    }
                }

                @Override // android.speech.tts.UtteranceProgressListener
                public void onError(String str2) {
                    TtsPlayListener ttsPlayListener2 = ttsPlayListener;
                    if (ttsPlayListener2 != null) {
                        ttsPlayListener2.onFailed(str2);
                    }
                }
            });
            this.mSpeakMessage = str;
            this.mListener = ttsPlayListener;
            this.mHandler.postDelayed(this.checkTtsRunnable, 1000L);
            this.mTextToSpeech.speak(str, 0, hashMap);
            return;
        }
        if (ttsPlayListener != null) {
            this.mHandler.post(new Runnable() { // from class: com.hx.lib_common.utils.TtsUtils.3
                @Override // java.lang.Runnable
                public void run() {
                    ttsPlayListener.onFailed(str);
                }
            });
        }
    }

    public void stopSpeak() {
        TextToSpeech textToSpeech = this.mTextToSpeech;
        if (textToSpeech != null) {
            textToSpeech.stop();
        }
    }

    public void release() {
        TextToSpeech textToSpeech = this.mTextToSpeech;
        if (textToSpeech != null) {
            textToSpeech.shutdown();
        }
    }
}
